#include "resources.h"
#include "toolbox.h"

Toolbox::Toolbox(){
    _stage = 0;
    _renderImg = al_create_bitmap(160, DISPLAY_H);
    _current_block = 0;

    dir[0] = Resources::TYPE_BLOCK_NORTH;
    dir[1] = Resources::TYPE_BLOCK_NORTH;
    dir[2] = Resources::TYPE_BLOCK_NORTH;
    dir[3] = Resources::TYPE_BLOCK_NORTH;

    imgs_block[0] = new Wall(dir[0] , dir[1] , dir[2] , dir[3] );
    imgs_block[1] = new Box(dir[0] , dir[1] , dir[2] , dir[3] );
    imgs_block[2] = new Target();
    imgs_block[3] = new Empty();
    imgs_block[4] = new Floor();

    _block = new Box(dir[0] , dir[1] , dir[2] , dir[3] );
}

Toolbox::ToolboxEvents Toolbox::onClick(int x, int y, int button){


    int x_d = (x / BLOCK_W) + 1;
    int y_d = (y / BLOCK_H) + 1;

    // Verificando cliques

    if(x_d == 1){
        if(y_d == 4) return onClickStageDown();
        if(y_d == 7) return onClickUndo();
        if(y_d == 9) return onClickChangeWest();
    }

    if(x_d == 2){
        if(y_d == 5) return onClickSaveStage();
        if(y_d == 7) return onClickUndo();
        if(y_d == 8) return onClickChangeNorth();
        if(y_d == 9) return onClickChangeBlock(button);
        if(y_d == 10) return onClickChangeSouth();
    }

    if(x_d == 3){
        if(y_d == 4) return onClickStageUp();
        if(y_d == 5) return onClickSaveStage();
        if(y_d == 7) return onClickDo();
        if(y_d == 9) return onClickChangeEast();
    }

    if(x_d == 4){
        if(y_d == 4) return onClickNewStage();
        if(y_d == 7) return onClickDo();
    }

    return Toolbox::NONE_EVENT;
}

Toolbox::ToolboxEvents Toolbox::onClickStageUp(){
    if( _stage < Resources::instance()->num_stages()-1 ){
        _stage++;
        return Toolbox::STAGE_UP;
    }
    return Toolbox::NONE_EVENT;
}

Toolbox::ToolboxEvents Toolbox::onClickStageDown(){
    if(_stage > 0){
        _stage--;
        return Toolbox::STAGE_DOWN;
    }
    return Toolbox::NONE_EVENT;
}

Toolbox::ToolboxEvents Toolbox::onClickNewStage(){
    return Toolbox::NEW_STAGE;
}

Toolbox::ToolboxEvents Toolbox::onClickSaveStage(){
    return Toolbox::SAVE_STAGE;
}

Toolbox::ToolboxEvents Toolbox::onClickDo(){
    return Toolbox::DO;
}

Toolbox::ToolboxEvents Toolbox::onClickUndo(){
    return Toolbox::UNDO;
}

Toolbox::ToolboxEvents Toolbox::onClickChangeNorth(){
    dir[0] = nextDir(dir[0]);
    changeDir();
    return Toolbox::CHANGE_NORTH;
}

Toolbox::ToolboxEvents Toolbox::onClickChangeEast(){
    dir[1] = nextDir(dir[1]);
    changeDir();
    return Toolbox::CHANGE_EAST;
}

Toolbox::ToolboxEvents Toolbox::onClickChangeSouth(){
    dir[2] = nextDir(dir[2]);
    changeDir();
    return Toolbox::CHANGE_SOUTH;
}

Toolbox::ToolboxEvents Toolbox::onClickChangeWest(){
    dir[3] = nextDir(dir[3]);
    changeDir();
    return Toolbox::CHANGE_WEST;
}

Toolbox::ToolboxEvents Toolbox::onClickChangeBlock(int button){
    int type = _block->type();

    if(button==1){
        _current_block = (_current_block+1)%5;
    }else{
        _current_block = (5+_current_block-1)%5;
    }

    _block = imgs_block[_current_block];
    return Toolbox::CHANGE_BLOCK;
}

void Toolbox::changeDir(){
    int type = _block->type();
    if(type == BOX){
        _block = new Box(dir[0] , dir[1] , dir[2] , dir[3] );
    } else if(type == WALL){
        _block = new Wall(dir[0] , dir[1] , dir[2] , dir[3] );
    }

}


int Toolbox::nextDir(int dir){
    if(dir == Resources::TYPE_BLOCK_NORTH)
        return Resources::TYPE_BLOCK_SOUTH;
    if(dir == Resources::TYPE_BLOCK_SOUTH)
        return Resources::TYPE_BLOCK_IRON;
    if(dir == Resources::TYPE_BLOCK_IRON)
        return Resources::TYPE_BLOCK_NONE;
    if(dir == Resources::TYPE_BLOCK_NONE)
        return Resources::TYPE_BLOCK_NORTH;

    return dir;
}


ALLEGRO_BITMAP* Toolbox::render(){
    ALLEGRO_BITMAP* lastBmp = al_get_target_bitmap();
    al_set_target_bitmap(_renderImg);
    al_clear_to_color(al_map_rgb(128, 128, 255));

    al_draw_bitmap(Resources::instance()->image( Resources::TOOLBOX ),0,0,0);

    char cp_stage[8];
    al_draw_text( Resources::instance()->font(Resources::FONT_SUPER_MARIO), al_map_rgb(0,0,0), BLOCK_W * 1 + 20,BLOCK_H *3 + 10, ALLEGRO_ALIGN_CENTRE, itoa(_stage+1,cp_stage,10));


    al_draw_bitmap(_block->render(),BLOCK_W * 1, BLOCK_H * 8,0);


    al_set_target_bitmap(lastBmp);
    return _renderImg;

}
